<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.nation.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-row :gutter="20">
            <el-col :span="5">
              <el-form-item :label="i18n('business.nation.flag.picture')" prop="picture">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.adornUrl('/file/picture')"
                  :show-file-list="false"
                  :headers="upload_headers"
                  :on-success="handlePictureSuccess"
                  :before-upload="beforePictureUpload">
                  <img v-if="dataForm.picture" :src="dataForm.picture" class="avatar-upload"/>
                  <el-icon v-else class="avatar-uploader-icon"><Plus/></el-icon>
                </el-upload>
              </el-form-item>
            </el-col>
            <el-col :span="19">
              <el-form-item :label="i18n('business.nation.chinese_name')" prop="chinese_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.region.chinese_name')"
                  v-model="dataForm.chinese_name" show-word-limit maxlength="50"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.english_name')" prop="english_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.english_name')" v-model="dataForm.english_name" show-word-limit maxlength="64"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.chinese_short_name')" prop="chinese_short_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.chinese_short_name')" v-model="dataForm.chinese_short_name" show-word-limit maxlength="20"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.chinese_alias')" prop="chinese_alias">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.chinese_alias')" v-model="dataForm.chinese_alias" show-word-limit maxlength="20"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.english_short_name')" prop="english_short_name">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.english_short_name')" v-model="dataForm.english_short_name" show-word-limit maxlength="64"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.english_alias')" prop="english_alias">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.english_alias')" v-model="dataForm.english_alias" show-word-limit maxlength="50"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.english_second_alias')" prop="english_second_alias">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.english_second_alias')" v-model="dataForm.english_second_alias" show-word-limit maxlength="50"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.alpah_2_code')" prop="alpah_2_code">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.alpah_2_code')" v-model="dataForm.alpah_2_code" show-word-limit maxlength="10"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.alpah_3_code')" prop="alpah_3_code">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.alpah_3_code')" v-model="dataForm.alpah_3_code" show-word-limit maxlength="10"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('business.nation.numeric')" prop="numeric">
                <el-input :placeholder="i18n('common.please_input') + i18n('business.nation.numeric')" v-model="dataForm.numeric" show-word-limit maxlength="4"></el-input>
              </el-form-item>

              <el-form-item :label="i18n('common.sort')" prop="sort">
                <el-input-number :placeholder="i18n('common.please_input') + i18n('common.sort')" v-model="dataForm.sort" :min="0"></el-input-number>
              </el-form-item>

              <el-form-item :label="i18n('common.status')" prop="status">
                <el-switch
                  v-model="dataForm.status"
                  active-value="1"
                  :active-text="i18n('common.enable')"
                  inactive-value="2"
                  :inactive-text="i18n('common.disable')">
                </el-switch>
              </el-form-item>

              <el-form-item>
                <el-button v-if="isAuth('module:business:nation:handle')" type="primary" @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-nation-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { useUpload } from "@/hooks/upload/useUpload"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/nation')

  const dataForm = reactive({
    id: 0,
    chinese_name: '',
    english_name: '',
    chinese_short_name: '',
    english_short_name: '',
    chinese_alias: '',
    english_alias: '',
    english_second_alias: '',
    alpah_2_code: '',
    alpah_3_code: '',
    numeric: '',
    picture: '',
    sort: 0,
    status: '1',
  })

  const dataRule = reactive({
    chinese_name: [{
      required: true,
      message: i18n('business.nation.rules.chinese_name.require'),
      trigger: 'blur',
    }],
    english_name: [{
      required: true,
      message: i18n('business.nation.rules.english_name.require'),
      trigger: 'blur',
    }],
    picture: [{
      required: true,
      message: i18n('business.nation.flag.rules.picture.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const {
    upload_headers,
    handlePictureSuccess,
    beforePictureUpload,
  } = useUpload(dataForm, '')

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.chinese_name = data.data.chinese_name
            dataForm.english_name = data.data.english_name
            dataForm.chinese_short_name = data.data.chinese_short_name
            dataForm.english_short_name = data.data.english_short_name
            dataForm.chinese_alias = data.data.chinese_alias
            dataForm.english_alias = data.data.english_alias
            dataForm.english_second_alias = data.data.english_second_alias
            dataForm.alpah_2_code = data.data.alpah_2_code
            dataForm.alpah_3_code = data.data.alpah_3_code
            dataForm.numeric = data.data.numeric
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/flag/data`),
              method: 'get',
              params: $http.adornParams({
                nation_id: dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.picture = data.data.picture
              }
            })
          }
        })
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
